﻿using MyCourseManageProject.Bll;
using MyCourseManageProject.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Configuration;
using System.Configuration;

namespace MyCourseManageProject.UI
{
    public partial class FrmAdminLogin : Form
    {
        public FrmAdminLogin()
        {
            InitializeComponent();
         

        }
        TeachersBll tbll = new TeachersBll();
       
        private void btnLoginSys_Click(object sender, EventArgs e)
        {
            string LoginId = this.comboBox1.Text.Trim();
            string Loginpwd = this.txtLoginPwd.Text.Trim();
            
 
            
          
            if (string.IsNullOrEmpty(LoginId))
            {
                MessageBox.Show("账号不能为空!", "提示信息");
                this.comboBox1.Focus();
                return;
            }
            if (string.IsNullOrEmpty(Loginpwd))
            {
                     MessageBox.Show("密码不能为空!","提示信息");
               this.txtLoginPwd.Focus();
                return;
            }
            // ps:实际开发中,还可以进一步验证邮箱  密码长度,非法信息
            //2封装登录实体信息(讲师信息)
                Teachers teacher = new Teachers()
                {
                    LoginAccount=this.comboBox1.Text.Trim(),
                    LoginPwd=this.txtLoginPwd.Text.Trim()
                };
            //3调用后台Bll方法完成验证(可以在此处加一个异常处理try-caher)
                teacher = tbll.TeacherLogin(teacher);
            //4验证登录是否成功
                if (teacher != null)
                {
                    //4.1如果登录成功,首先保存登录信息
                    Program.currentTeacher = teacher;
                    //实际开发中还要设置账号:
                        //有效性(在保存登录信息之前完成)
                        //用户权限的保存
                        //登录日志保存
                        //是否在一定时间内保存用户信息(把讲师对象通过序列化保存)
                    //4.2设置窗体返回值
                    this.DialogResult = DialogResult.OK;
                    this.Close();

                }
                else
                {
                    MessageBox.Show("账号或密码错误,请重新输入","登录提示");
                }


        }
        /// <summary>
        /// 记住密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Teachers user = new Teachers();
              // 登录时 如果没有Data.bin文件就创建、有就打开
              FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
           
              BinaryFormatter bf = new BinaryFormatter();
             
              // 保存在实体类属性中
              user.LoginAccount = comboBox1.Text.Trim();
              //保存密码选中状态
             if (chkRemember.Checked)
                 user.LoginPwd = txtLoginPwd.Text.Trim();
            else
                 user.LoginPwd = "";

             //选在集合中是否存在用户名 
             if (users.ContainsKey(user.LoginAccount))
             {
                 //如果有清掉
                 users.Remove(user.LoginAccount);
             }
             //添加用户信息到集合
             users.Add(user.LoginAccount, user);
             //写入文件
             bf.Serialize(fs, users);
           
             //关闭
             fs.Close();
            
         } 
        


        Dictionary<string, Teachers> users = new Dictionary<string, Teachers>();
        private void FrmAdminLogin_Load(object sender, EventArgs e)
        {
           
            //读取文件流对象
              FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
              if (fs.Length > 0)
              {
                  BinaryFormatter bf = new BinaryFormatter();
                  //读出存在Data.bin 里的用户信息
                 users = bf.Deserialize(fs) as Dictionary<string, Teachers>;
                 //循环添加到Combox1
                 foreach (Teachers user in users.Values)
                 {
                     comboBox1.Items.Add(user.LoginAccount);                
                 }

                 //combox1 用户名默认选中第一个
                 if (comboBox1.Items.Count > 0)
                    comboBox1.SelectedIndex = comboBox1.Items.Count-1;
            }
             fs.Close();
         }

        private void btnOut_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        }

  
    
}
